package cn.santi.controller;

import cn.santi.domain.JActivities;
import cn.santi.emuns.R;
import cn.santi.service.JActivitiesService;
import cn.santi.utli.DateTool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Activities")
public class ActivitiesController {

    @Autowired
    private JActivitiesService jActivitiesService;

    @GetMapping("/getActivitiesById")
    public R getActivitiesById(Integer uid, Integer status) {//根据uid查询记录历史或是我的收藏
//        System.out.println("uid:" + uid + "status:" + status);
        LambdaQueryWrapper<JActivities> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JActivities::getUid, uid)
                .eq(JActivities::getStatus, status)
                .orderByDesc(JActivities::getADate);
        List<JActivities> list = jActivitiesService.list(wrapper);
        return R.ok(list);
    }

    @GetMapping("/deleteActivitiesById")
    public R deleteActivitiesById(Integer aid) {//根据aid删除我的收藏记录
//        System.out.println("aid:" + aid);
        boolean b = jActivitiesService.removeById(aid);
        if (b) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @GetMapping("/getisActivities")
    public R getisActivities(JActivities jActivities) {//设置默认显示是否收藏
//        System.out.println("getisActivities:" + jActivities);
        LambdaQueryWrapper<JActivities> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JActivities::getUid, jActivities.getUid())
                .eq(JActivities::getJzidorid, jActivities.getJzidorid())//活动id
                .eq(JActivities::getType, jActivities.getType())
                .eq(JActivities::getStatus, jActivities.getStatus());//状态是0是历史记录，1是收藏记录

        JActivities one = jActivitiesService.getOne(wrapper);
//        System.out.println("getisActivitiesone:" + one);
        if (one != null) {
            return R.ok("已收藏");
        } else {
            return R.fail("未收藏");
        }
    }

    @GetMapping("/delActivities")
    public R delActivities(JActivities jActivities) {//取消我的收藏
//        System.out.println("jActivities:" + jActivities);
        LambdaQueryWrapper<JActivities> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JActivities::getUid, jActivities.getUid())//用户id
                .eq(JActivities::getJzidorid, jActivities.getJzidorid())//活动id
                .eq(JActivities::getType, jActivities.getType())
                .eq(JActivities::getStatus, jActivities.getStatus());//状态是0是历史记录，1是收藏记录

        boolean remove = jActivitiesService.remove(wrapper);

        if (remove) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @GetMapping("/addActivities")
    public R addActivities(JActivities jActivities) {//添加历史记录或是我的收藏
//        System.out.println("jActivities:" + jActivities);

        jActivities.setADate(DateTool.gettime());

        LambdaQueryWrapper<JActivities> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JActivities::getUid, jActivities.getUid())//用户id
                .eq(JActivities::getJzidorid, jActivities.getJzidorid())//活动id
                .eq(JActivities::getType, jActivities.getType())
                .eq(JActivities::getStatus, jActivities.getStatus());//状态是0是历史记录，1是收藏记录

        JActivities one = jActivitiesService.getOne(wrapper);
        if (one != null) {
            one.setADate(DateTool.gettime());
            jActivitiesService.updateById(one);
            return R.fail("已存在并修改");
        } else {
            jActivitiesService.save(jActivities);
            return R.ok("添加成功");
        }
    }

}
